/**
 * There are arthur code generation
 */
package com.arthur.shopping.controller;

import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arthur.common.annotation.Log;
import com.arthur.common.controller.BaseController;
import com.arthur.common.domain.QueryRequest;
import com.arthur.common.domain.ResponseBo;
import com.arthur.common.util.DynamicSpecifications;
import com.arthur.common.util.FileUtils;
import com.arthur.shopping.domain.Goods;
import com.arthur.shopping.service.GoodsService;
import com.arthur.shopping.service.TypeGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;


@Controller
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private TypeGoodsService typeGoodsService;
	
	@RequestMapping("shopping/goods")
	public String index(Model model) {
		return "shopping/goods/list";
	}
	
	@Log("新增商品")
	@RequiresPermissions("goods:add")
	@RequestMapping("shopping/goods/add")
	@ResponseBody
	public ResponseBo addGoods(Goods goods , String parentId) {
		try {
			this.goodsService.save(goods,parentId);
			return ResponseBo.ok("新增商品成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增商品失败，请联系网站管理员！");
		}
	}

	@Log("修改商品")
	@RequiresPermissions("goods:update")
	@RequestMapping("shopping/goods/update")
	@ResponseBody
	public ResponseBo updateGoods(Goods goods , String parentId) {
		try {
			this.goodsService.updateNotNull(goods,parentId);
			return ResponseBo.ok("修改商品成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改商品失败，请联系网站管理员！");
		}
	}
	
	@Log("删除商品")
	@RequiresPermissions("goods:delete")
	@RequestMapping("shopping/goods/delete")
	@ResponseBody
	public ResponseBo deleteGoodss(String ids) {
		try {
			this.goodsService.deletes(ids);
			return ResponseBo.ok("删除商品成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除商品失败，请联系网站管理员！");
		}
	}
	
	@RequestMapping("shopping/goods/getGoods")
	@ResponseBody
	public ResponseBo getGoods(Long id) {
		try {
			Goods goods = this.goodsService.selectByKey(id);
			
			String typeids = typeGoodsService.selectGoodsTypeIdStr(id);
			
			
			return ResponseBo.ok(goods).put("parentId", typeids);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取商品信息失败，请联系网站管理员！");
		}
	}
	
	@RequestMapping("shopping/goods/list")
	@ResponseBody
	public Map<String, Object> goodsList(ServletRequest request, QueryRequest queryRequest, Goods goods) {
		PageHelper.startPage(queryRequest.getPageNum(), queryRequest.getPageSize());
		Example example = DynamicSpecifications.bySearchFilter(request,Goods.class,goods);
		List<Goods> list = this.goodsService.selectByExample(example);
		PageInfo<Goods> pageInfo = new PageInfo<Goods>(list);
		return getDataTable(pageInfo);
	}
	
	
	@RequestMapping("shopping/goods/excel")
	@ResponseBody
	public ResponseBo goodsExcel(ServletRequest request, Goods goods) {
		try {
			Example example = DynamicSpecifications.bySearchFilter(request,Goods.class,goods);
			List<Goods> list = this.goodsService.selectByExample(example);
			return FileUtils.createExcelByPOIKit("商品表", list, Goods.class);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("导出Excel失败，请联系网站管理员！");
		}
	}

	@RequestMapping("shopping/goods/csv")
	@ResponseBody
	public ResponseBo goodsCsv(ServletRequest request, Goods goods) {
		try {
			Example example = DynamicSpecifications.bySearchFilter(request,Goods.class,goods);
			List<Goods> list = this.goodsService.selectByExample(example);
			return FileUtils.createCsv("商品表", list, Goods.class);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("导出Csv失败，请联系网站管理员！");
		}
	}

}

